{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Translate an Alien Message\n",
        "\n",
        "## Introduction\n",
        "In this beginner-friendly coding challenge you’ll learn all about strings and how characters are strung together to form strings! Strings are an important data type used by software developers every day. Today, we’ll be using the C# (pronounced see-sharp) language to translate an alien message to English!\n",
        "\n",
        "### Learning Objectives\n",
        "1. Learn what a string is  \n",
        "1. Create string data \n",
        "1. Manipulate string data into a new string with concatenation and interpolation \n",
        "\n",
        "### A quick overview of useful terms used in this notebook\n",
        "1. [String](https://docs.microsoft.com/dotnet/api/system.string?view=net-6.0)\n",
        "2. [Character](https://docs.microsoft.com/dotnet/api/system.char?view=net-6.0) \n",
        "3. [Variable](https://docs.microsoft.com/dotnet/csharp/language-reference/language-specification/variables)\n",
        "4. [Console](https://docs.microsoft.com/windows/console/consoles)\n",
        "5. [Switch Expression](https://docs.microsoft.com/dotnet/csharp/language-reference/operators/switch-expression)\n",
        "6. [foreach Loop](https://docs.microsoft.com/dotnet/csharp/language-reference/statements/iteration-statements#the-foreach-statement)\n",
        "7. [String Interpolation](https://docs.microsoft.com/dotnet/csharp/language-reference/tokens/interpolated?msclkid=1b008d82d0b611ec96aa6fdaf77cbe48)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "![Alien Image](https://raw.githubusercontent.com/katiesavage/csharp-notebooks/main/csharp-scenarios/images/alien-ship.png)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Scenario\n",
        "\n",
        "Imagine you are a computational linguist hired by the government to help communicate with a group of Aliens that recently landed on Earth. These extraterrestrials speak a non-human language that called language X (for extraterrestrial). Luckily, your linguistics partner arrived at the scene before you and was able to generate English translations for some basic words in language X. She has determined that the aliens want to give humans a message. As her computational linguist counterpart, it is your job to **translate longer messages in language X to English using code**.  \n",
        "\n",
        "A **Computational Linguist** uses computing and artificial intelligence to study natural languages and natural language patterns. These linguists utilize language data to model different languages and they can apply these models in many different ways. Tools like chat bots, speech recognition systems, and search engines are all build with the help of computational linguistics. "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Console.WriteLine\n",
        "### Introduce yourself to the aliens\n",
        "\n",
        "When you first arrive at the scene, your linguistic partner encourages you to introduce yourself to the aliens. Because it will be a quick introduction, she’ll translate this statement for you. \n",
        "\n",
        "To do this you’ll need to write a string that displays to the console.  \n",
        "\n",
        "A **string** is a sequence of characters. Strings are a particularly useful data type that allow you to interact with users of your program. In C#, strings start and end with a quotation mark. For example, “this is a string”.  \n",
        "\n",
        "To greet the aliens, replace *myName* in the below code with your name. Then, run the code.    "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "source": [
        "Console.WriteLine(\"Hi extraterrestrials, my name is myName.\");"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": "Hi extraterrestrials, my name is myName.\r\n"
          },
          "execution_count": 1,
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The *Console.WriteLine()* [statement](https://docs.microsoft.com/dotnet/csharp/programming-guide/statements-expressions-operators/statements) allows us to write our string to the console. Let’s break this down. \n",
        "\n",
        "> * The [Console](https://docs.microsoft.com/dotnet/api/system.console?view=net-6.0) is what you can use to output strings. \n",
        "> * The [WriteLine() method](https://docs.microsoft.com/dotnet/api/system.console.writeline?view=net-6.0) writes whatever data you specify in the parenthesis to the output. "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Using variables with strings\n",
        "### Tell the aliens more about yourself\n",
        "Amazing job! Your linguistic partner says you should introduce yourself a bit more so the aliens know what you do. But, she says to only refer to yourself using your name instead of a pronoun like I, because she can’t translate pronouns yet. Replace all the instances of *myName* with your name and hit run. "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "source": [
        "Console.WriteLine(\"Hi extraterrestrials, my name is myName.\"); \n",
        "Console.WriteLine(\"myName is a researcher.\"); \n",
        "Console.WriteLine(\"myName studies language.\"); "
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": "myName studies language.\r\n"
          },
          "execution_count": 1,
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Introduce the aliens to others\n",
        "Great job! Now other members of your research team want to use your code to introduce themselves. But, going through and changing all the *myName*s with the name of each researcher is tedious and is likely to cause a mistake. Notice how in the strings we want to output, the only thing that changes is the myName. One way to simplify our code and reduce the room for error is by using a variable and string interpolation. A **variable** is a symbol you can use to run the same code with different values. \n",
        "\n",
        "Instead of replacing every *myName*, let’s create a string variable called name that we can assign to a different name one time and it will update all the myNames throughout the introduction! \n",
        "\n",
        "We will also need to use **string interpolation** to use the variable in our string. This allows you to construct a single result string from a string that includes embedded code. Every interpolated string has two parts.\n",
        "1. The string begins with the `$` immediately before the opening quotation mark. \n",
        "2. At least one interpolated expression. You must wrap these interpolated expressions in curly braces `{` `}`. You can put any C# expression that returns a value inside the braces. In this exercise, we're using a string. \n",
        "\n",
        "Try running the example below to see how it works."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "source": [
        "var phoneNumber = 123456789;\n",
        "Console.WriteLine($\"For more help, please call {phoneNumber}\");"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": "For more help, please call 123456789\r\n"
          },
          "execution_count": 1,
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In this example, the phoneNumber variable was embedded into the string.\n",
        "\n",
        "Now let's try this with our message to the aliens. In the code below, create a variable called `name` and assign it the value \"myName\". Then, using the `$`, `{`, and `}`, turn the three statements into interpolated expressions."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "source": [
        "Console.WriteLine(\"Hi extraterrestrials, my name is myName.\"); \n",
        "Console.WriteLine(\"myName is a researcher.\"); \n",
        "Console.WriteLine(\"myName studies language.\"); "
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": "myName studies language.\r\n"
          },
          "execution_count": 1,
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now, you can set name to the name of whoever is being introduced. Try replacing *myName* with the name Joon and running it.  \n",
        "\n",
        "The above lines with Console.WriteLine are **string literals**, which means it is text that represents a constant string. "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Let's translate a message\n",
        "It’s time to start translating. Your linguistic partner gives you the following graph that includes all the words that she’s been able to translate in language X and their English translation. We’re going to use this data to translate the alien messages. \n",
        "\n",
        "### Language data\n",
        "This data is organized into a [switch expression](https://docs.microsoft.com/dotnet/csharp/language-reference/operators/switch-expression). This **switch expression** takes in an alien word as input, and outputs the English translation. In general, a switch expression matches the input against a series of cases and the output is dependent upon which case is matched. In order to translate the message, we’ll use this switch expression to help us match the alien words and receive the proper English word back. "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "source": [
        "string ToEnglish(string word) => word switch\n",
        "{\n",
        "    \"yoish\"     => \"the aliens\",\n",
        "    \"ceroyash\"  => \"Earth\",\n",
        "    \"nomshce\"   => \"want\",\n",
        "    \"warshel\"   => \"please\",\n",
        "    \"yoishel\"   => \"Alien planet\",\n",
        "    \"oppor\"     => \"need\",\n",
        "    \"afala\"     => \"trees\",\n",
        "    \"eshteve\"   => \"walk\",\n",
        "    \"blarg\"     => \"speak\",\n",
        "    \"shioy\"     => \"humans\",\n",
        "    \"abree\"     => \"help\",\n",
        "    \"shwaye\"    => \"crashed\",\n",
        "    \"ni\"        => \"on\",\n",
        "    \"coierm\"    => \"to go\",\n",
        "    \"lleog\"     => \"dirt\",\n",
        "    \"efellesh\"  => \"peace\",\n",
        "    \"veiesht\"   => \"home\",\n",
        "    \"shir\"      => \"can\",\n",
        "    \"argsha\"    => \"spaceship\",\n",
        "    _           => \"Unknown\"\n",
        "};"
      ],
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "As you can see, each Language X word above is mapped to a word in English. For example, \"Yoish\" means \"the aliens\"."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Looks like the aliens are sending their first message:\n",
        "### \"warshel abree yoish\" "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "For our first translation, let’s replace each language X word with its English word counterpart. To do this, we’ll use the [String.Replace method](https://docs.microsoft.com/dotnet/api/system.string.replace?view=net-6.0). To use this method correctly, we pass in the string we want to replace and the replacement string. Run the cell below to see how it works. \n",
        "\n",
        "Here's an example:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "source": [
        "var stringName = \"He is cold\"; \n",
        "\n",
        "// Below, we replace \"He\" with \"She\" and \"cold\" with \"cool\"\n",
        "stringName = stringName.Replace(\"He\", \"She\").Replace(\"cold\",\"cool\"); \n",
        "\n",
        "Console.WriteLine(stringName);"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": "She is cool\r\n"
          },
          "execution_count": 1,
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ">Notice how you can put multiple Replaces in one statement\n",
        "\n",
        "Now, let's try this out with the alien messsage."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "source": [
        "// here’s the alien message \n",
        "\n",
        "var message = \"Warshel abree yoish\";\n",
        "\n",
        "// try replacing the words in language x with their English counterparts using the Replace method \n",
        "\n",
        "// print the message\n",
        "Console.WriteLine(message);"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": "Warshel abree yoish\r\n"
          },
          "execution_count": 1,
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Let's use loops\n",
        "\n",
        "Looks like a second alien message is incoming:\n",
        "### Yoish shwaye ni ceroyash. Yoish oppor shioy abree coierm veiesht. Shir shioy abree yoish? \n",
        "Replacing words one by one is a bit tedious for longer messages like this. We can make this process more efficient. Imagine if we could write a piece of code that goes through an unknown alien message word-by-word, finds the English translation for each word, and outputs the translated English message. Luckily we can by using loops. **Loops** are a way to repeat an action multiple times. \n",
        "\n",
        "We'll use the ToEnglish **switch expression** above to translate each Language X word in the alien message to English."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Before we can loop over the words in the sentence, we need to break up the message string into words. To do this, we will use the [String.Split()](https://docs.microsoft.com/dotnet/api/system.string.split?view=net-6.0) method. We pass the String.Split() method a delineating character that the method will use to split the string into substrings. In the case of a sentence, we can choose a space ‘ ‘ as the delineating character because each word is separated by a space. In the cell below, use String.Split() to split up the sentence string into word substrings. "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "![Split Method Image](https://raw.githubusercontent.com/katiesavage/csharp-notebooks/main/csharp-scenarios/images/split-method.png)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now that we can split our sentence string into word substrings, it’s time to loop over them and translate! To do this, we’ll use a [for loop](https://docs.microsoft.com/dotnet/csharp/language-reference/statements/iteration-statements#the-for-statement). This is a special type of loop that executes a statement for a specified number of times. \n",
        "\n",
        "The syntax works like this: "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "source": [
        "var sentence = \"this is an example\";\n",
        "\n",
        "// split the string into substrings\n",
        "string[] variableCollection = sentence.Split(' ');\n",
        "\n",
        "for(var i = 0; i < variableCollection.Length; i++) \n",
        "{ \n",
        "     // statements to be executed \n",
        "     Console.Write(variableCollection[i] + \" \");\n",
        "} "
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": "example "
          },
          "execution_count": 1,
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ">The **data type** in our case will be strings. The **variable name** can be anything, but to ensure our code is understandable, let’s call it word. And the **collection variable** will our list of language X words. The statements to be executed must be wrapped in curly brackets. \n",
        "\n",
        "Now that you can split the alien sentence strings into word substrings and we know how to loop through these strings, it’s time to translate the alien message!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "source": [
        "var alienMessage = \"yoish shwaye ni ceroyash yoish oppor shioy abree coierm veiesht shir shioy abree yoish\"; \n",
        "\n",
        "// split alienMessage into word substrings and store it in a variable called words \n",
        "string[] words = alienMessage.Split(' '); \n",
        "\n",
        "// Here is where we will loop through each word in words and translate them  \n",
        "for(var i = 0; i < words.Length; i++) \n",
        "{ \n",
        "\t// pass the word to the switch expression here \n",
        "}\n",
        "\n",
        "// turn the English word back into a string using the Join method\n",
        "var englishMessage = string.Join(\" \", words);\n",
        "Console.WriteLine(englishMessage);"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": "yoish shwaye ni ceroyash yoish oppor shioy abree coierm veiesht shir shioy abree yoish\r\n"
          },
          "execution_count": 1,
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Great job! You can now relay this message as well as your translation tool to the Alien Response Team so they can get the aliens home. Mission succeeded!"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".NET (C#)",
      "language": "C#",
      "name": ".net-csharp"
    },
    "language_info": {
      "file_extension": ".cs",
      "mimetype": "text/x-csharp",
      "name": "C#",
      "pygments_lexer": "csharp",
      "version": "8.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}